package com.ihighest.app.infox.adapter;

import io.rong.imkit.RongIM;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ihighest.app.common.kit.AppKit;
import com.ihighest.app.common.kit.SharedPreferencesKit;
import com.ihighest.app.common.kit.ToastKit;
import com.ihighest.app.infox.Const;
import com.ihighest.app.infox.R;
import com.ihighest.app.infox.activity.AskQuestionActivity;
import com.ihighest.app.infox.activity.LoginActivity;
import com.ihighest.app.infox.activity.SpecialistOnlineActivity;
import com.ihighest.app.infox.activity.UserDetailActivity;

/**
 * 专家在线
 * 
 * @author Vity Created at 2015-10-22 上午12:26:33
 * @version 1.0
 */
public class SpecialistOnlineListViewAdapter extends BaseAdapter {

	private static final String TAG = "SpecialistOnlineListViewAdapter";
	Context mContext;
	JSONArray array = new JSONArray();
	LayoutInflater inflater;

	// ImageLoader imageLoader = ImageLoader.getInstance();

	public SpecialistOnlineListViewAdapter(Context context) {
		this.mContext = context;
		this.inflater = LayoutInflater.from(context);
	}

	public void setDatas(JSONArray jsonArray) {
		this.array = jsonArray;
	}

	public void appendDatas(JSONArray jsonArray) {
		try {
			this.array = AppKit.joinJSONArray(array, jsonArray);
		} catch (JSONException e) {
			Log.e(TAG, e.getMessage(), e);
		}
	}

	@Override
	public int getCount() {
		return array == null ? 0 : array.length();
	}

	@Override
	public Object getItem(int arg0) {
		return arg0;
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@SuppressLint("InflateParams")
	@Override
	public View getView(final int position, View view, ViewGroup arg2) {
		ViewHolder viewHolder = null;
		if (view == null) {
			viewHolder = new ViewHolder();
			view = LayoutInflater.from(mContext).inflate(R.layout.infox_activity_specialist_online_item, null);
			viewHolder.tvTitle = (TextView) view.findViewById(R.id.title);
			viewHolder.image = (ImageView) view.findViewById(R.id.imageView1);
			viewHolder.iv_chat = (ImageView) view.findViewById(R.id.iv_chat);
			viewHolder.iv_rankImage = (ImageView) view.findViewById(R.id.iv_rankImage);
			viewHolder.tvRank = (TextView) view.findViewById(R.id.textView1);
			// viewHolder.askTa = (TextView) view.findViewById(R.id.askTA);
			viewHolder.askTa = (TextView) view.findViewById(R.id.askTA);
			// viewHolder.itemParentLayout = (RelativeLayout)
			// view.findViewById(R.id.itemParentLauout);
			viewHolder.specialOutline = (ImageView) view.findViewById(R.id.specialistOutLine);
			// viewHolder.isOnline = (TextView) view.findViewById(R.id.outline);
			view.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) view.getTag();
		}

		try {
			final JSONObject json = array.getJSONObject(position);

			viewHolder.tvTitle.setText(json.getString("title"));

			// DisplayImageOptions defaultOptions = new
			// DisplayImageOptions.Builder().showImageForEmptyUri(R.drawable.infox_person_no_icon)
			// .showImageOnFail(R.drawable.infox_person_no_icon).showImageOnLoading(R.drawable.infox_person_no_icon)
			// .cacheInMemory(true).cacheOnDisc(true).build();
			// ImageLoader.getInstance().displayImage(json.getString("image"),
			// viewHolder.image, defaultOptions);
			// ImageLoader.getInstance().displayImage(json.getString("rankImage"),
			// viewHolder.iv_rankImage, defaultOptions);
			AppKit.SetUserImg(json.getString("image"), viewHolder.image);
			AppKit.SetNormalImg(json.getString("rankImage"), viewHolder.iv_rankImage);
			viewHolder.tvRank.setText(json.getString("rank"));
			final String id = json.getString("uid");
			viewHolder.image.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {
					Intent intent = new Intent(mContext, UserDetailActivity.class);
					intent.putExtra("userId", id);
					mContext.startActivity(intent);

				}
			});
			viewHolder.askTa.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {
					Intent intent = new Intent(mContext, AskQuestionActivity.class);
					intent.putExtra("userId", id);
					mContext.startActivity(intent);

				}
			});
			if (json.getString("isOnline").endsWith("0")) {
				// viewHolder.itemParentLayout.setBackgroundColor(Color.WHITE);
				// viewHolder.specialOutline.setVisibility(View.GONE);

				// viewHolder.isOnline.setVisibility(View.GONE);
			} else {
				ColorMatrix matrix = new ColorMatrix();
				matrix.setSaturation(0);
				ColorMatrixColorFilter filter = new ColorMatrixColorFilter(matrix);
				viewHolder.image.setColorFilter(filter);
				// viewHolder.isOnline.setVisibility(View.VISIBLE);
				// viewHolder.specialOutline.setVisibility(View.VISIBLE);
			}
			if (TextUtils.isEmpty(json.getString("uid"))) {
				viewHolder.iv_chat.setVisibility(View.GONE);
			} else {
				viewHolder.iv_chat.setVisibility(View.VISIBLE);
				viewHolder.iv_chat.setTag(R.id.tag_1, json.getString("rid"));
				viewHolder.iv_chat.setTag(R.id.tag_2, json.getString("uid"));
				viewHolder.iv_chat.setTag(R.id.tag_3, json.getString("title"));
				viewHolder.iv_chat.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View arg0) {
						if (TextUtils.isEmpty(SharedPreferencesKit.getString(mContext, Const.SP_KEY_USER_ID))) {
							((Activity) mContext).startActivity(new Intent(mContext, LoginActivity.class));
							ToastKit.showShort(mContext, Const.UN_LOGIN_ALERT);
						} else {
							Const.IM_EXTRA = "speci_chat:" + ((String) arg0.getTag(R.id.tag_2)) + ":"
									+ SharedPreferencesKit.getString(mContext, Const.SP_KEY_USER_MID);
							RongIM.getInstance().startPrivateChat(mContext, ((String) arg0.getTag(R.id.tag_1)),
									((String) arg0.getTag(R.id.tag_3)));
						}
					}
				});
			}
		} catch (Exception ex) {
			// pass
		}
		return view;
	}

	final static class ViewHolder {
		TextView tvTitle;
		ImageView image, iv_chat, iv_rankImage;
		TextView tvRank;
		TextView askTa;
		// RelativeLayout itemParentLayout;
		// TextView isOnline;
		ImageView specialOutline;

	}
}
